<?php
	/*
	 * File: config_inc.php
	 * Last modification: 20.07.2011
	 * Author(s): kivio.wanderley
	 * 
	 */
?>
	
<?php /** MAIN PHP CODE */

	function config_set_global_array() {
		global $g_config_global_array, $g_core_dir;
		
		require_once( $g_core_dir . 'config_defaults' .'.php' ); //Require the text file
				
		$t_vars = get_defined_vars(); //Get all defined variables

		foreach ( array_keys( $t_vars ) as $t_var ) {
			$t_config_var = preg_replace( '[^c_]', '', $t_var );

			if ( $t_config_var != $t_var ) {
				$g_config_global_array[ $t_config_var ] = $$t_var; //Dinamic var with $$
			}
		}
		
		return $g_config_global_array;
	}
	
	function config_get_global_array() {
		global $g_config_global_array;
		
		if( !isset($g_config_global_array) ) {
			return config_set_global_array();
		} else {
			return $g_config_global_array;
		}
		
		trigger_error("ERRO ON GETTING CONFIGURATION GLOBAL ARRAY", E_USER_ERROR);
	}
	
	/**
	 * Verify if the configuration file exists.
	 * @return true if it exists or false otherwise
	 */
	function config_is_set() {
		global $g_core_dir;
		
		return file_exists( $g_core_dir . 'config_defaults' .'.php' );
	}
	
	/**
	 * Unset the global variable of configuration
	 * @param (bool) $p_all
	 */
	function config_unset($p_all = false) {
		global $g_config_global_array;
		
		if($p_all)
			$g_config_global_array = null;
		else {
			$g_config_global_array['host'] = null;
		    $g_config_global_array['port'] = null;
		    $g_config_global_array['database'] = null;
		    $g_config_global_array['user'] = null;
		    $g_config_global_array['password'] = null;
		}
		
	}

	/**
	 * Open the configuration of defaults values file.
	 * @return Pointer to the file.
	 */
//	function config_load_file() {
//		global $g_core_dir, $g_arq_name, $g_arq_pointer, $g_config_global_array;
//		
//		/* If it is already set */
//		if( isset($g_arq_pointer) ) return $g_arq_pointer;
//		
//		$g_arq_name = ($g_core_dir."config_defaults.php"); //File name
//		$g_arq_pointer = @fopen($g_arq_name,"r"); //Open to read
//		
//		# If file exists create global array with information
//		if( $g_arq_pointer ) {
//			rewind($g_arq_pointer);
//			
//			$t_data_array = explode(',', fread($g_arq_pointer, filesize($g_arq_name) ) );
//			foreach ( $t_data_array as $value ) {
//				$t_data = explode('=', $value);
//				$g_config_global_array[$t_data[0]] = $t_data[1];
//			}
//		}
//		
//		return $g_arq_pointer;
//	}
	
	
	
	/**
	 * Close the configuration file pointer.
	 */
//	function config_close_file() {
//		global $g_arq_pointer;
//		
//		fclose($g_arq_pointer);
//	}
	
	/**
	 * Destroy the configuration bin file.
	 */
//	function config_destroy_file() {
//		global $g_arq_pointer;
//		
//		
//	}
	
	/**
	 * Get an information from the configuration file.
	 * @param (string) $p_info Information to get;
	 * @return The value on configuration file or an erro.
	 */
	function config_get( $p_info ) {
		global $g_config_global_array;
		
		if( !config_is_set() ) {
			trigger_error("ERRO ON CONFIGURATION DEFAULT NOT FOUND", E_USER_ERROR);
		}

		if( ! isset($g_config_global_array) ) {
			trigger_error("ERRO ON GETTING CONFIGURATION ", E_USER_ERROR);
		}
		
		foreach ( $g_config_global_array as $key => $value ) {
			if( $key == $p_info ) {
				return $value;
			}
		}
		
//		trigger_error("ERRO ON GETTING CONFIGURATION", E_USER_ERROR);
	}

	/* ========================================
	 * Permissions management
	 * ========================================
	 */
		
	function config_create_permissions( $p_manage_options_array ) {
		foreach ($p_manage_options_array as $title=>$items) {
			if($erro ) break;
			foreach ($items as $title) {
				$title = datab_prepare_string('title', $title);
				
				$t_query = "INSERT INTO tb_configuracao (conf_id, conf_titulo, conf_valores)
							VALUES (nextval('sq_tb_configuracao'), $title, NULL);";
				$t_result = datab_query($t_query);	
				
				# If result got en error
				if(is_string($t_result)) { set_info_msg($t_result, I_ERR); }
			}
		}
	}
	
	function config_save_permissions( $p_manage_options_array, $p_post_array ) {
		foreach ($p_manage_options_array as $title=>$items) {
			foreach ($items as $title) {
				$t_values = ($p_post_array[ $title ]);
				if(datab_prepare_isnull($t_values)) { 
					$t_values = "NULL"; 
				} else {
					$t_values = serialize($t_values);
					$t_values = "'".$t_values."'";
				}
				$title = datab_prepare_string('title', $title);
				
				$t_query = "UPDATE tb_configuracao
							SET conf_valores = $t_values
							WHERE conf_titulo = $title;";
				$t_result = datab_query($t_query);	
				
				# If result got en error
				if(is_string($t_result)) { set_info_msg($t_result, I_ERR); }
			}
		}
	}
	
	function config_get_permissions( $p_title ) {
		$p_title = datab_prepare_string('title', $p_title);
		
		$t_query = "SELECT conf_valores AS values
					FROM tb_configuracao
					WHERE conf_titulo = $p_title;";
		$t_result = datab_query($t_query);	
		
		# If result got en error
		if(is_string($t_result)) { set_info_msg($t_result, I_ERR); }

		# If result got more than one result
		if( sizeof($t_result) > 1 ) { set_info_msg("MORE THAN ONE RESULT ON PERMISSION SELECT", I_ERR); }
		
		return unserialize($t_result[0]['values']);
	}
	
	/* ========================================
	 * Install
	 * ========================================
	 */
	
	/**
	 * Write the given infromation on the configuration file.
	 * @param (string) $p_host Host of the database;
	 * @param (string) $p_port The port to connect to database;
	 * @param (string) $p_dbname Database name; 
	 * @param (string) $p_user User name for database;
	 * @param (string) $p_pswd Password for database;
	 * @param (string) $p_dlang Language of the strings on site;
	 */
	function config_write_defaults($p_uri_prefix = null, $p_host = null, $p_port = null, $p_dbname = null, $p_user = null, $p_pswd = null, 
								   $p_dlang = null, $p_permissions = null, 
								   $p_smtp_port, $p_smtp_host, $p_smtp_secure_type, $p_smtp_auth, $p_smtp_user, $p_smtp_pass, $p_email_admin)
	{
		global $g_arq_name, $g_core_dir;

		$g_arq_name = ($g_core_dir."config_defaults.php");
		
		if($p_host == null) 	{ $p_host = config_get('host'); }
		if($p_port == null) 	{ $p_port = config_get('port'); }
		if($p_dbname == null) 	{ $p_dbname = config_get('database'); }
		if($p_user == null) 	{ $p_user = config_get('user'); }
		if($p_pswd == null) 	{ $p_pswd = config_get('password'); }
		if($p_dlang == null) 	{ $p_dlang = config_get('language'); }
		# URI prefix
		$uri_prefix = '
# Prefix used by redirections
$c_uri_prefix="'.$p_uri_prefix.'";
';
		# Data base connection data
		$conn_data = '
# Database connection information
$c_host="'.$p_host.'";
$c_port="'.$p_port.'";
$c_database="'.$p_dbname.'";
$c_user="'.$p_user.'";
$c_password="'.$p_pswd. '";
';
		# Default language data
		$lang_data = '
# Language used by default
$c_language="'.$p_dlang. '";
';
		# E-Mail data
		$p_smtp_auth = gpc_string_to_bool($p_smtp_auth) == false ? 'false' : 'true';
		
		$email_data = '
# E-Mail configuration
$c_smtp_port="'.$p_smtp_port. '";
$c_smtp_host="'.$p_smtp_host. '";
$c_smtp_secure_type="'.$p_smtp_secure_type. '";
$c_smtp_auth='.$p_smtp_auth.';
$c_smtp_user="'.$p_smtp_user. '";
$c_smtp_pass="'.$p_smtp_pass. '";
$c_email_admin="'.$p_email_admin. '";
';
		# Permissions management
		$permissions_string = '
# Permissions titles allowed
$c_permissions = array(';
		foreach ($p_permissions as $title=>$arr) {
			$permissions_string .= '"'.$title.'" => array(';
			foreach ($arr as $value) {
				$permissions_string .= '"'.$value.'",';
			}	
			$permissions_string .= '),';
		}
		$permissions_string .= ');
';
	
		$permissions_string = str_replace(',)', ')', $permissions_string);
		$permissions_string = str_replace('),)', '))', $permissions_string);
		
		# Write on file
		$t_arq_pointer = fopen($g_arq_name,"w");
		fwrite($t_arq_pointer, 
"<?php 
". $uri_prefix.$conn_data.$lang_data.$email_data.$permissions_string ." 
?>");

		fclose($t_arq_pointer);
	}
	
?>